package com.uchengwang.ui.cm_ui.me.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.ui.AddClubMemberActivity;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/7/5.
 * 社团加人
 */

public class AddClubMemberAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<UserBean> list = new ArrayList<>();
    public AddClubMemberAdapter(Context context){
        this.context = context;
    }

    public void setData(List<UserBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<UserBean> getData(){
        return list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_add_club_member, parent, false);
        return new AddClubVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        AddClubVH holder = (AddClubVH) h;

        final UserBean bean = list.get(position);

        String headUrl = bean.getUserAvatar();
        String nickName = bean.getNickName();
        String sign = bean.getUserSign();

        if (!TextUtils.isEmpty(headUrl)){
            ImageManager.getInstance().loadImage(context, holder.head, headUrl, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }

        holder.name.setText(nickName);
        holder.des.setText(sign);

        boolean isMem = isMember(String.valueOf(bean.getId()));
        if (isMem){
            holder.checkBox.setVisibility(View.INVISIBLE);
        }else {
            holder.checkBox.setVisibility(View.VISIBLE);
            holder.checkBox.setChecked(bean.isCheck());

            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    bean.setCheck(b);
                }
            });

        }



        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class AddClubVH extends RecyclerView.ViewHolder{

        private View itemView;
        private ImageView head;
        private TextView name, des;
        private AppCompatCheckBox checkBox;
        private Button add_mem;


        public AddClubVH(View view) {
            super(view);
            AutoUtils.autoSize(view);
            itemView = view;
            head = (ImageView) view.findViewById(R.id.add_image);
            name = (TextView) view.findViewById(R.id.add_name);
            des = (TextView) view.findViewById(R.id.add_info);
            add_mem = (Button) view.findViewById(R.id.add_mem);

            checkBox = (AppCompatCheckBox) view.findViewById(R.id.add_checkBox);
        }
    }

    /**
     * 判断用户是否已经是社团成员 true 是成员
     * */
    public boolean isMember(String uid){
        if (AddClubMemberActivity.membersUids != null && AddClubMemberActivity.membersUids.size() >0){
            int length = AddClubMemberActivity.membersUids.size();
            for (int i=0; i<length; i++){
                if (uid.equals(AddClubMemberActivity.membersUids.get(i))){
                    return true;
                }
            }
        }

        return false;
    }

}
